package com.flca.frw.zk.util;

import java.math.BigDecimal;
import java.util.Date;

import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Decimalbox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Radio;
import org.zkoss.zul.Radiogroup;
import org.zkoss.zul.Window;
import org.zkoss.zul.impl.InputElement;

import com.flca.frw.util.TransformUtils;

public abstract class WindowBase extends Window{

	private static final long serialVersionUID = -7499545593989183640L;

	protected TransformUtils tu = TransformUtils.getInstance();
	protected WindowHelper wh;
	
	public static final boolean UPD_VIEW = true;
	public static final boolean UPD_MODEL = false;
	
	
	public WindowBase() {
		super();
		wh = new WindowHelper(this);
	}

	public WindowBase(String title, String border, boolean closable) {
		super(title, border, closable);
		wh = new WindowHelper(this);
	}

	/**
	 * Shortcut to get the String value of zk field indicated by aCompname
	 * @param aCompname
	 * @return
	 */
	public String getString(String aCompname) 
	{
		Component comp = getComponent(aCompname);
		if (comp instanceof InputElement) {
			return ((InputElement) comp).getRawText();
		} 

		return null;
	}

	public Object getComboBoxValue(String aCompname) 
	{
		Component comp = getComponent(aCompname);
		if (comp instanceof Combobox) {
			Combobox cb = (Combobox) comp;
			Comboitem selcitem = cb.getSelectedItem();
			if (selcitem != null) {
				return selcitem.getValue();
			}
		} else {
			throw new RuntimeException(aCompname + " is not a Combobox");
		}

		return null;
	}
	
	/**
	 * Gets the value from a checkbox
	 * @param aCompname
	 * @return
	 */
	public boolean getBooleanValue(String aCompname) 
	{
		Component comp = getComponent(aCompname);
		if (comp instanceof Checkbox) {
			Checkbox cb = (Checkbox) comp;
			return cb.isChecked();
		} else {
			throw new RuntimeException(aCompname + " is not a Checkbox");
		}
	}
	
	public Object getRadiogroupValue(String aCompname) 
	{
		Component comp = getComponent(aCompname);
		if (comp instanceof Radiogroup) {
			Radiogroup radio = (Radiogroup) comp;
			Radio selcitem = radio.getSelectedItem();
			if (selcitem != null) {
				return selcitem.getValue();
			}
		} else {
			throw new RuntimeException(aCompname + " is not a Radiogroup");
		}

		return null;
	}


	/**
	 * Shortcut to get the BigDecimal value of zk field indicated by aCompname
	 * @param aCompname
	 * @return
	 */
	public BigDecimal getBigDecimal(String aCompname) 
	{
		Component comp = getComponent(aCompname);
		if (comp instanceof Decimalbox) {
			return ((Decimalbox) comp).getValue();
		} else {
			return null;
		}
	}

	public Date getDate(String aCompname) 
	{
		Component comp = getComponent(aCompname);
		if (comp instanceof Datebox) {
			return ((Datebox) comp).getValue();
		} else {
			return null;
		}
	}

	public Integer getInteger(String aCompname) 
	{
		Component comp = getComponent(aCompname);
		if (comp instanceof Intbox) {
			return ((Intbox) comp).getValue();
		} else {
			return null;
		}
	}

	public Component getComponent(String aCompname) 
	{
		return wh.getComponent(aCompname);
	}
	
	
	public void updateViewFromModel() 
	{
		updateModelFromToView(true);
		updateView();
	}

	public void updateModelFromView() 
	{
		if (validate()) {
			updateModelFromToView(false);
			updateModel();	
		}
	}
	
	protected abstract void updateView();
	protected abstract void updateModel();
	public abstract boolean validate();
	
	protected abstract void updateModelFromToView(boolean aUpdateView);


}
